package com.baizan.daka.ui.presenter

import com.baizan.daka.base.BasePresenter
import com.baizan.daka.ui.contract.HomeFrgContract
import com.baizan.daka.mvp.model.BannerModel
import com.baizan.daka.mvp.model.MeModel
import com.baizan.daka.mvp.model.NewsModel
import com.baizan.daka.mvp.model.TaskModel
import com.baizan.daka.mvp.model.bean.*
import com.baizan.daka.net.BasePageResponse
import com.baizan.daka.rx.Subscriber.CommonSubscriber
import com.baizan.daka.showToast

class HomeFrgPresenter(val mView: HomeFrgContract.View) : BasePresenter(),
    HomeFrgContract.Presenter {

    val taskModel: TaskModel by lazy { TaskModel() }
      val newsModel: NewsModel by lazy { NewsModel() }
      val bannerModel: BannerModel by lazy { BannerModel() }
    val meModel: MeModel by lazy { MeModel() }

    override fun loadList(CurrentPage: Int) {
        mView.showLoadingDialog()
        taskModel.list(CurrentPage)
            .subscribe(object :
                CommonSubscriber<BasePageResponse<TaskInfoBean>>(compositeDisposable) {

                override fun onCallNext(t: BasePageResponse<TaskInfoBean>?) {
                    mView.loadListSuccess(t!!)
                }

                override fun onCallError(e: String) {
                    super.onCallError(e)
                    showToast(e)
                }

                override fun onCallCompleted() {
                    super.onCallCompleted()
                    mView.hideLoadingDialog()
                }
            })

    }

    override fun loadNewsList() {
        newsModel.loadNoticeList()
            .subscribe(object :
                CommonSubscriber<List<NoticeInfoBean>>(compositeDisposable) {

                override fun onCallNext(t: List<NoticeInfoBean>?) {
                    mView.loadNewsListSuccess(t!!)
                }

                override fun onCallError(e: String) {
                    super.onCallError(e)
                    mView.loadNewsListFail()
                }

                override fun onCallCompleted() {
                    super.onCallCompleted()
                    mView.hideLoadingDialog()
                }
            })
    }

    override fun loadBanner() {
        bannerModel.loadHomeBanner()
            .subscribe(object :
            CommonSubscriber<List<BannerBean>>(compositeDisposable) {

            override fun onCallNext(t: List<BannerBean>?) {
                mView.loadBannerSuccess(t!!)
            }

            override fun onCallError(e: String) {
                super.onCallError(e)
                mView.loadBannerFail()
            }

            override fun onCallCompleted() {
                super.onCallCompleted()
                mView.hideLoadingDialog()
            }
        })
    }

    override fun receiveTask(index:Int,taskId: Long) {
        mView.showLoadingDialog()
        taskModel.receivedTask(taskId)
            .subscribe(object :
                CommonSubscriber<Any>(compositeDisposable) {

                override fun onCallNext(t: Any?) {
                    mView.receiveTaskSuccess(index,t!!)
                }

                override fun onCallError(e: String) {
                    super.onCallError(e)
                    showToast(e)
                }

                override fun onCallCompleted() {
                    super.onCallCompleted()
                    mView.hideLoadingDialog()
                }
            })
    }

    override fun loaduserPackageDes() {
        mView.showLoadingDialog()
        meModel.userPackageDes()
            .subscribe(object :
                CommonSubscriber<UserPackageDes>(compositeDisposable) {

                override fun onCallNext(t: UserPackageDes?) {
                    mView.loadUserPackageDesSuccess(t!!)
                }

                override fun onCallError(e: String) {
                    super.onCallError(e)
                    mView.loadUserFial(e)
                }

                override fun onCallCompleted() {
                    super.onCallCompleted()
                    mView.hideLoadingDialog()
                }
            })
    }


}

